package com.base.cn.platform.os.controller.course.comment;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.comment.CourseCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 课程评论接口Controller
 *
 * @author s.li
 * @create 2018-09-07-14:36
 */
@RestController
@RequestMapping("/api/comment")
public class ApiCourseCommentController extends ApiBaseController {

    @Autowired
    private CourseCommentService courseCommentService;

    /**
     * 查询课程评论列表
     * @param courseId 课程ID
     * @param catalogId 目录ID（可不传）
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/listComment")
    public Map<String,Object> listComment(@RequestParam("courseId") BigDecimal courseId,
                                          @RequestParam(value = "catalogId",required = false,defaultValue = "0") BigDecimal catalogId,
                                          @RequestParam("currentPage") int currentPage){
        Map<String,Object> comment = new HashMap<>();
        comment.put("courseId",courseId);
        comment.put("catalogId",catalogId);
        comment.put("status", 1);
        Map<String,Object> commentMap = courseCommentService.findCourseCommentPage(comment,currentPage,10,true,false,false);
        return ResultUtil.SUCCESS(commentMap);
    }

    /**
     * 保存评论
     * @param courseId 购买的套餐ID或课程ID
     * @param userId 用户ID
     * @param catalogId 目录ID
     * @param comment 评论内容
     * @param quality 课程质量(1~5)
     * @param teach 老师讲法(1~5)
     * @param feel 综合感受(1~5)
     * @return Map<String,Object>
     */
    @RequestMapping("/saveComment")
    public Map<String,Object> saveComment(@RequestParam("courseId") BigDecimal courseId,
                                          @RequestParam("userId") BigDecimal userId,
                                          @RequestParam("catalogId") BigDecimal catalogId,
                                          @RequestParam("comment") String comment,
                                          @RequestParam(value = "quality",required = false,defaultValue = "0") int quality,
                                          @RequestParam(value = "teach",required = false,defaultValue = "0") int teach,
                                          @RequestParam(value = "feel",required = false,defaultValue = "0") int feel) throws Exception {
        comment = URLDecoder.decode(comment,"utf-8");
        Map<String,Object> commentMap = new HashMap<>();
        commentMap.put("courseId",courseId);
        commentMap.put("userId",userId);
        commentMap.put("catalogId",catalogId);
        commentMap.put("comment",comment);
        commentMap.put("quality",quality);
        commentMap.put("teach",teach);
        commentMap.put("feel",feel);
        return courseCommentService.saveCourseComment(commentMap);
    }
}
